<?php

namespace app\admin\controller;

use app\admin\service\OssService;
use app\BaseController;
use app\admin\model\FileOss as FileOssModal;
use OSS\Core\OssException;

class Oss extends BaseController
{
    protected $middleware = [
        'Auth' => ['except' => ['index']],
    ];
    public function index()
    {
        $ossInfo = new OssService();
//        $info = $ossInfo->alc('uploads/20240801/66ab2c6891896.jpg');
//        echo $info;
        $url = $ossInfo->signPath('uploads/20240801/66ab2c6891896.jpg');
        echo $url;
    }
    public function list() {
        $params = $this->request->post(['current', 'size', 'name']);
        $sql = new FileOssModal();
        $list = $sql
            ->order('create_at desc')
            ->with(['aboutPhotoUser', 'aboutPohtoInstitution', 'aboutBanner', 'aboutComment', 'aboutVersion'])
            ->paginate([
                'list_rows'=> $params['size'],
                'page' => $params['current'],
            ]);
        $ossInfo = new OssService();
        foreach ($list as $key => $item) {
            $item['temporary_path'] = $ossInfo->signPath($item->delPath);
            $item['alc'] = $ossInfo->getAlc($item->delPath);
            if($item->photo_id) {
                $item['about_id'] = $item->photo_id;
                $item['about_type'] = '套图角色头像';
                continue;
            }
            if($item->institution_id) {
                $item['about_id'] = $item->institution_id;
                $item['about_type'] = '机构头像';
                continue;
            }
            if($item->image_id) {
                $item['about_id'] = $item->image_id;
                $item['about_type'] = '轮播图';
                continue;
            }
            if($item->comment_id) {
                $item['about_id'] = $item->comment_id;
                $item['about_type'] = '背景图片';
                continue;
            }
            if($item->fileId_id) {
                $item['about_id'] = $item->fileId_id;
                $item['about_type'] = '版本号: '.$item->versionName;
                continue;
            }
            $item['about_type'] = '未使用';
        }
        return result()->success($list);
    }
    public function alc()
    {
        $params = $this->request->post(['delPath', 'alc']);
        $ossInfo = new OssService();
        $ossInfo->setAlc($params['delPath'], $params['alc']);
        return result()->success('设置成功');
    }
    public function upload() {
        $file = request()->file('file');
        if (!$file) {
            return result()->fail('请选择要上传的文件', 400);
        }
        try {
            $ossInfo = new OssService();
            $info = $ossInfo->upload($file, $this->request->post('alt', ''));
            $sizeSizeMb = $file->getSize() / 1024 / 1024;
            $params = [
                'userId' => $this->request->user->id,
                'delPath' => $info['path'],
                'type' => 2,
                'fileSize' =>  $sizeSizeMb,
            ];
            $sql = new FileOssModal();
            $sql->save($params);
            return result()->success([
                'id' => $sql->id,
                'url' => $ossInfo->signId($sql->id),
            ]);
        } catch (OssException $e) {
            return  result()->fail($e->getMessage(), 500);
        }
    }

    public function localupload()
    {
        $files = request()->file();
        try {
            validate(['file' => [
                'fileSize' => 20 * 1024 * 1024,
                'fileExt' => 'gif,jpg,png'
            ]])->check(['file' => $files]);
            $savename = [];
            foreach ($files as $file) {
                $savename[] = URL . UPLOADIMAGE . str_replace("\\", "/", \think\facade\Filesystem::disk('public')->putFile('img', $file));
            }
            return result()->success($savename);
        } catch (\think\exception\ValidateException $e) {
            result()->fail("上传失败", 500);
        }
    }

    public function del() {
        $params = $this->request->post(['id']);
        $sql = new FileOssModal();
        $info = $sql->where('id', $params['id'])->findOrEmpty();
        if($info->isEmpty()) {
            return result()->fail('文件不存在', 400);
        }
        $ossInfo = new OssService();
        $ossInfo->del($info->delPath);
        $info->delete();
        return result()->success('删除成功');
    }
}